package com.ruoyi.user.service.impl;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.user.domain.TbUser;
import com.ruoyi.user.domain.TbUserProfile;
import com.ruoyi.user.mapper.TbUser1Mapper;
import com.ruoyi.user.service.ITbUser1Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 管理用户Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-29
 */
@Service
public class TbUser1ServiceImpl implements ITbUser1Service
{
    @Autowired
    private TbUser1Mapper tbUserMapper;

    /**
     * 查询管理用户
     * 
     * @param userId 管理用户主键
     * @return 管理用户
     */
    @Override
    public TbUser selectTbUserByUserId(Long userId)
    {
        return tbUserMapper.selectTbUserByUserId(userId);
    }

    /**
     * 查询管理用户列表
     * 
     * @param tbUser 管理用户
     * @return 管理用户
     */
    @Override
    public List<TbUser> selectTbUserList(TbUser tbUser)
    {
        return tbUserMapper.selectTbUserList(tbUser);
    }

    /**
     * 新增管理用户
     * 
     * @param tbUser 管理用户
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTbUser(TbUser tbUser)
    {
        int rows = tbUserMapper.insertTbUser(tbUser);
        insertTbUserProfile(tbUser);
        return rows;
    }

    /**
     * 修改管理用户
     * 
     * @param tbUser 管理用户
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTbUser(TbUser tbUser)
    {
        tbUserMapper.deleteTbUserProfileByUserId(tbUser.getUserId());
        insertTbUserProfile(tbUser);
        return tbUserMapper.updateTbUser(tbUser);
    }

    /**
     * 批量删除管理用户
     * 
     * @param userIds 需要删除的管理用户主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTbUserByUserIds(Long[] userIds)
    {
        tbUserMapper.deleteTbUserProfileByUserIds(userIds);
        return tbUserMapper.deleteTbUserByUserIds(userIds);
    }

    /**
     * 删除管理用户信息
     * 
     * @param userId 管理用户主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTbUserByUserId(Long userId)
    {
        tbUserMapper.deleteTbUserProfileByUserId(userId);
        return tbUserMapper.deleteTbUserByUserId(userId);
    }

    /**
     * 新增用户详细资料信息
     * 
     * @param tbUser 管理用户对象
     */
    public void insertTbUserProfile(TbUser tbUser)
    {
        List<TbUserProfile> tbUserProfileList = tbUser.getTbUserProfileList();
        Long userId = tbUser.getUserId();
        if (StringUtils.isNotNull(tbUserProfileList))
        {
            List<TbUserProfile> list = new ArrayList<TbUserProfile>();
            for (TbUserProfile tbUserProfile : tbUserProfileList)
            {
                tbUserProfile.setUserId(userId);
                list.add(tbUserProfile);
            }
            if (list.size() > 0)
            {
                tbUserMapper.batchTbUserProfile(list);
            }
        }
    }
}
